const mongoose = require("mongoose");
const db = mongoose.connect(
  "mongodb://localhost:27017/TechCodeKnowledgeStructure",
  {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  },
  (err) => {
    if (err) {
      console.log("===============================");
      console.log("数据库连接失败:", err);
      console.log("===============================");
      return;
    }
    console.log("数据库连接成功");
  }
);

var Schema = mongoose.Schema;

var blogSchema = new Schema({
  title: String,
  author: String,
  body: String,
  comments: [{ body: String, date: Date }],
  date: { type: Date, default: Date.now },
  hidden: Boolean,
  meta: {
    votes: Number,
    favs: Number,
  },
});
// 增
const Blog = mongoose.model("Blog", blogSchema);

const blog = new Blog("数据对象");
blog
  .save()
  .then((res) => res)
  .catch((err) => {
    console.log("插入失败" + err);
    return false;
  });
//删
// 方法1:
blog.remove / deleteone / deleteMany("条件对象", (err) => db.close());
// 方法2(推荐）
blog
  .deleteone("条件对象")
  .then((res) => {
    return res.deletedcount;
  })
  .catch((err) => {
    console.log("删除失败" + err);
    return false;
  });

// 改
